package com.hh85.mp3;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private TextView playText,pauseText;
    private int pos = 0;//播放到的位置
    private int allNum = 0;
    private ImageView imageView;
    private  Animation mAnimation;
    private ProgressBar progressBar;
    private TextView progressText;
    private Boolean isPlaying = false;
    private Timer mTimer;
    private TimerTask mTimerTask;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.id_cover);
        mAnimation = AnimationUtils.loadAnimation(this, R.anim.animore);
        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
        mAnimation.setInterpolator(lin);
        mediaPlayer = MediaPlayer.create(this, R.raw.blg);
        pauseText = (TextView)findViewById(R.id.id_pause);
        playText = (TextView)findViewById(R.id.id_play);
        allNum =   mediaPlayer.getDuration();
        //点击播放按钮
        playText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               play();
            }
        });
        //点击暂停按钮
        pauseText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer !=null){
                    isPlaying = false;
                    mediaPlayer.pause();
                    pos =  mediaPlayer.getCurrentPosition();
                    mAnimation.cancel();
                    imageView.clearAnimation();
                }

            }
        });
        //进度条
        progressBar = (ProgressBar)findViewById(R.id.id_progressBar);
        progressBar.setProgress(0);
        progressBar.setMax(mediaPlayer.getDuration());
        //进度条提示控件
        progressText = (TextView)findViewById(R.id.id_progressTip);
        progressText.setText( pos + "/" + allNum);


        //----------定时器记录播放进度---------//
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                if(isPlaying==true) {
                   MainActivity.this.runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           pos = mediaPlayer.getCurrentPosition();
                           progressBar.setProgress(pos);
                           float p = (float)pos/allNum;
                           int jindu = (int)(p * 100);
                           progressText.setText("播放进度:"+ pos + "/" + allNum + "："+jindu +"%");
                       }
                   });
                }

            }
        };
        mTimer.schedule(mTimerTask, 0, 10);

    }

    private void play(){
        if(mediaPlayer != null){
            isPlaying = true;
            //跳转到之前保存的播放进度
            mediaPlayer.seekTo(pos);
            //开始播放
            mediaPlayer.start();
            //开始播放滚动动画
            imageView.startAnimation(mAnimation);
        }

    }
}
